/*
 *
 *
 *
 */

#include "display.h"

Display::Display(){
	list = (unsigned int *)memalign(16, 262144*4);
	intraFontInit();
	font = intraFontLoad("flash0:/font/ltn4.pgf", 0);
	info = intraFontLoad("flash0:/font/ltn0.pgf", 0);
	
	setupGu();

}
void Display::setClockFontStyle(float size, unsigned int colour, unsigned int shadow, unsigned int options){
	intraFontSetStyle(font, size, colour, shadow, 0);
}
void Display::setInfoFontStyle(float size, unsigned int colour, unsigned int shadow, unsigned int options){
	intraFontSetStyle(info, size, colour, shadow, 0);
}
float Display::clockFontMeasureText(const char *text){
	return intraFontMeasureText(font, text);
}
float Display::clockPrint(float x, float y, const char *text){
	return intraFontPrint(font, x, y, text);
}
float Display::infoPrint(float x, float y, const char *text){
	return intraFontPrint(info, x, y, text);
}
void Display::setupGu(){
    sceGuInit();
	sceGuStart(GU_DIRECT, list);

	sceGuDrawBuffer(GU_PSM_8888, (void*)0, BUF_WIDTH);
	sceGuDispBuffer(SCR_WIDTH, SCR_HEIGHT, (void*)0x88000, BUF_WIDTH);
	sceGuDepthBuffer((void*)0x110000, BUF_WIDTH);
 
	sceGuOffset(2048 - (SCR_WIDTH/2), 2048 - (SCR_HEIGHT/2));
	sceGuViewport(2048, 2048, SCR_WIDTH, SCR_HEIGHT);
	sceGuDepthRange(65535, 0);
	sceGuScissor(0, 0, SCR_WIDTH, SCR_HEIGHT);
	sceGuEnable(GU_SCISSOR_TEST);
	sceGuDepthFunc(GU_GEQUAL);
	sceGuEnable(GU_DEPTH_TEST);
	sceGuFrontFace(GU_CW);
	sceGuShadeModel(GU_SMOOTH);
	sceGuEnable(GU_CULL_FACE);
	sceGuEnable(GU_CLIP_PLANES);
	sceGuEnable(GU_BLEND);
	sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
	sceGuFinish();
	sceGuSync(0,0);
 
	sceDisplayWaitVblankStart();
	sceGuDisplay(GU_TRUE);

}

void Display::guStart(){
	if (drawing) return;
	sceGuStart(GU_DIRECT, list);
	sceGuClearColor(BLACK);
	sceGuClearDepth(0);
	sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);
	drawing = true;
}
void Display::guFinish(){
	if (!drawing) return;
	sceGuFinish();
	sceGuSync(0,0);
	sceDisplayWaitVblankStart();
	sceGuSwapBuffers();
	drawing = false;
}